import { Component } from '@angular/core';
import { HttpService } from './http.service';

function desc(target) {
  console.log('---------------类的装饰器参数 start------------------');
  console.log(target.prototype); // 输出 [Function: Person]表示当前装饰的类
  target.prototype.requestUrl = '/api';
  target.prototype.getData = function() {
    console.log(this);
  }
  console.log('---------------类的装饰器参数 end------------------');
  // return class NewComponent extends target {
  //   requestUrl = '/api';
  //   getData() {
  //     console.log('get');
  //   }
  //   constructor(){
  //     super();
  //   }
  // }

}

// function addLoading<T extends { new(...args: any[]): {} }>(constructor: T) {
//   return class extends constructor {
//     loading = false;
//   };
// }
function loading(...flags){
  return function load(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    const old = descriptor.value;
    descriptor.value = function(...args) {
      flags.map(flag => this[flag] = !this[flag]);
      const a = old.call(this, ...args);
      a.then(r => {
        flags.map(flag => this[flag] = !this[flag]);
      }, e => {
        flags.map(flag => this[flag] = !this[flag]);
      })
    }
  }
}

function enumerable(value: boolean) {
  return function name(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    // descriptor.enumerable = value;
    // console.log(target, propertyKey, descriptor);
    // return function newFn() {
      console.log(descriptor.value);
      const old = descriptor.value;
      descriptor.value = function(t) {
        console.log('test start', this, t);
        const r = old.call(this, t);
        r.then(res => {
          console.log(res);
        });
        console.log(r);
        console.log('test end');
      }
    // }
  };
}


// @classDecorator
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'desc';
  constructor(public http: HttpService) {

  }
  @enumerable(false)
  async test(value) {
    console.log(this)
    const r = await this.http.getData(true);
    // ...code
    return;
    // await 'end';
  }
}
